package
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	import mx.graphics.codec.JPEGEncoder;
	import mx.graphics.codec.PNGEncoder;
	
	
	/**
	 * 把分散的多张按钮图，制作LAYA多态按钮
	 */
	public class MakeLayaButton
	{
		private var _images:Array;
		private var _files:Array;
		private var _index:int;
		
		public function MakeLayaButton()
		{
		}
		public function doMake(files:Array):void
		{
			_files = files;
			
			_index = 0;
			_images = [];
			startLoad();
		}
		private function startLoad():void
		{
			if(_index >= _files.length)
			{
				makeHandler();
				return;
			}
			var file:File = _files[_index];
			
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleComplate);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, handleComplate);
			loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, handleHttpStatus);
			loader.load(new URLRequest(file.url));
		}
		private function makeHandler():void
		{
			var maxw:int = (_images[0] as BitmapData).width;
			var maxh:int = _images[0].height * _images.length;
			var packBmd:BitmapData = new BitmapData(maxw, maxh, true, 0);//新图片
			
			for(var i:int=0; i<_images.length; i++)
			{
				var bmd:BitmapData = _images[i];
				var curRct:Rectangle = new Rectangle(0, 0, bmd.width, bmd.height);
				var pos:Point = new Point(0, bmd.height * i);
				packBmd.copyPixels(bmd, curRct, pos);
			}
			writeImg(packBmd);
			
			packBmd.dispose();
		}
		public function writeImg(param1:BitmapData, index:int=0):void
		{
			var imgByteArray:ByteArray;
			var jpgenc:JPEGEncoder;
			var pngenc:PNGEncoder;
			var bmd:BitmapData = param1;
			
			pngenc = new PNGEncoder();
			imgByteArray = pngenc.encode(bmd);
			
			
			var outUrl:String = (_files[0] as File).url;
			if(outUrl.indexOf(".png") != -1)
			{
				outUrl = outUrl.replace(".png", "-合并.png");
			}
			else if(outUrl.indexOf(".bmp") != -1)
			{
				outUrl = outUrl.replace(".bmp", "-合并.png");
			}
			
			var fl:File = new File(outUrl);
			var fs:FileStream = new FileStream();
			try
			{
				fs.open(fl, FileMode.WRITE);
				fs.writeBytes(imgByteArray);
				fs.close();
			}
			catch(e:Error)
			{
				
			}
		}
		
		private function handleComplate(e:Event):void
		{
			var loadinfo:LoaderInfo = e.currentTarget as LoaderInfo;
			loadinfo.removeEventListener(Event.COMPLETE, handleComplate);
			loadinfo.removeEventListener(IOErrorEvent.IO_ERROR, handleComplate);
			loadinfo.removeEventListener(HTTPStatusEvent.HTTP_STATUS, handleHttpStatus);
			
			_images.push((loadinfo.content as Bitmap).bitmapData);
			_index ++;
			startLoad();
		}
		private function handleHttpStatus(e:*):void
		{
			
		}
	}
}